package cn.wp.ewallet.wallet.dao.impl;

import cn.wp.common.core.dao.SqlString;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.wallet.dao.MentionMoneyInfoDao;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import cn.wp.ewallet.wallet.model.entity.MentionMoneyInfoDo;
import cn.wp.ewallet.wallet.model.entity.MentionMoneyInfoDoo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import cn.wp.common.core.dao.JdbcSupportTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 提币申请单-数据访问层实现
 * Created by zhangyongli on 2020/01/09
 */
@Repository
public class MentionMoneyInfoDaoImpl implements MentionMoneyInfoDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public MentionMoneyInfoDoo selectMentionMoneyReqOrderById(String orderId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.*, p.oper_result, s.sys_user_code as oper_user, p.oper_time, p.remark as oper_remark, ");
        sqlString.add("o.oper_result as trans_result, o.address_from, o.address_to, o.trans_time, o.service_charge as transfer_service_charge");
        sqlString.add("from (");
        sqlString.add("    select r.*, m.mention_amount, m.service_charge, m.address, ac.money_type_Id, type.money_type_code");
        sqlString.add("    from req_order_info r, mention_money_info m, account_info ac, money_type_info type");
        sqlString.add("    where r.req_order_id = m.req_order_id");
        sqlString.add("        and ac.account_id = r.account_id and ac.money_type_Id = type.money_type_Id and r.req_order_id = ?");
        sqlString.add(") t");
        sqlString.add("    left join proc_record_info p on t.req_order_id = p.req_order_id");
        sqlString.add("    left join trans_account_log o on t.req_order_id = o.req_order_id");
        sqlString.add("    left JOIN system_user_info s on p.oper_user = s.sys_user_id");


        return jdbcSupportTemplate.selectObject(sqlString.build(), new Object[]{orderId}, MentionMoneyInfoDoo.class);
    }

    @Override
    public WpPage<MentionMoneyInfoDoo> selectMentionMoneyReqOrderByAddress(String address, WpPage page) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.*, p.oper_result, s.sys_user_code as oper_user, p.oper_time, p.remark as oper_remark,");
        sqlString.add("o.oper_result as trans_result, o.address_from, o.address_to, o.trans_time, o.service_charge as transfer_service_charge");
        sqlString.add("from (");
        sqlString.add("    select r.*, m.mention_amount, m.service_charge, m.address, a.money_type_id");
        sqlString.add("        , type.money_type_code");
        sqlString.add("    from req_order_info r, mention_money_info m, account_info a, money_type_info type");
        sqlString.add("    where r.req_order_id = m.req_order_id");
        sqlString.add("        and r.account_id = a.account_id");
        sqlString.add("        and a.money_type_Id = type.money_type_Id");
        sqlString.add("        and r.wallet_address = ?");
        sqlString.add("        and r.req_type = ?");
        sqlString.add(") t");
        sqlString.add("    left join proc_record_info p on t.req_order_id = p.req_order_id");
        sqlString.add("    left join trans_account_log o on t.req_order_id = o.req_order_id");
        sqlString.add("    left JOIN system_user_info s on p.oper_user = s.sys_user_id");
        sqlString.add("order by t.create_date desc");

        return jdbcSupportTemplate.selectList(sqlString.build(), new Object[]{address, EwalletWalletConstant.REQ_ORDER_TYPE_MENTIONMONEY}, page, MentionMoneyInfoDoo.class);
    }

    @Override
    public WpPage<MentionMoneyInfoDoo> getMentionMoneyReqOrderByMoneyType(String startTime, String endTime, String moneyType, WpPage page) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select t.*, p.oper_result, s.sys_user_code as oper_user, p.oper_time, p.remark as oper_remark,");
        sqlString.add("o.oper_result as trans_result, o.address_from, o.address_to, o.trans_time, o.service_charge as transfer_service_charge");
        sqlString.add("from (");
        sqlString.add("    select r.*, m.mention_amount, m.service_charge, m.address, a.money_type_id");
        sqlString.add("        , type.money_type_code, concat(p.mobile_prefix, '-', p.user_tel) as user_tel");
        sqlString.add("    from req_order_info r, mention_money_info m, account_info a, money_type_info type, user_wallet_rel u, pa_user_info p");
        sqlString.add("    where r.req_order_id = m.req_order_id");
        sqlString.add("        and r.account_id = a.account_id");
        sqlString.add("         and r.wallet_address = u.wallet_address");
        sqlString.add("           and u.user_id = p.user_id");
        sqlString.add("        and a.money_type_Id = type.money_type_Id");
        sqlString.add("        and a.money_type_Id = ?");
        sqlString.add("        and r.req_type = ?");
        sqlString.add(") t");
        sqlString.add("    left join proc_record_info p on t.req_order_id = p.req_order_id");
        sqlString.add("    left join trans_account_log o on t.req_order_id = o.req_order_id");
        sqlString.add("    left JOIN system_user_info s on p.oper_user = s.sys_user_id");
        sqlString.add("    where 1=1");
        List<String> params = new ArrayList<>();
        params.add(moneyType);
        params.add(EwalletWalletConstant.REQ_ORDER_TYPE_MENTIONMONEY);
        if (StringUtils.isNotEmpty(startTime)) {
            sqlString.add("and   t.create_date > ?");
            params.add(startTime);
        }
        if (StringUtils.isNotEmpty(endTime)) {
            sqlString.add("and t.create_date < ?");
            params.add(endTime);
        }
        sqlString.add("order by t.create_date desc");

        return jdbcSupportTemplate.selectList(sqlString.build(), params.toArray(), page, MentionMoneyInfoDoo.class);
    }

    /**
     * 保存 提币记录
     * @param mentionMoneyInfo
     * @return
     */
    @Override
    public int insertMentionMoneyInfo(MentionMoneyInfoDo mentionMoneyInfo) {
        return jdbcSupportTemplate.insert(mentionMoneyInfo);
    }
}